package com.qqbot.utils;

import com.alibaba.fastjson.JSONObject;
import com.qqbot.vo.TouchItVo;
import lombok.extern.slf4j.Slf4j;
import love.forte.simbot.ID;
import love.forte.simbot.component.mirai.message.SimbotOriginalMiraiMessage;
import love.forte.simbot.definition.Group;
import love.forte.simbot.definition.GroupMember;
import love.forte.simbot.event.GroupMessageEvent;
import love.forte.simbot.message.*;
import love.forte.simbot.resources.Resource;
import net.mamoe.mirai.message.data.LightApp;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * 工具类
 *
 * @author 付亚全
 */
@Slf4j
public class SimbotUtil {

    /**
     * 将String类型ID进行类型转换
     *
     * @param id
     * @return
     */
    public static ID getID(String id) {
        return ID.$(id);
    }

    /**
     * 根据消息获取被艾特人信息
     *
     * @param messageContent
     * @return
     */
    public static At getAtInfo(ReceivedMessageContent messageContent) {
        for (Message.Element<?> message : messageContent.getMessages()) {
            if (message instanceof At) {
                return (At) message;
            }
        }
        return null;
    }

    /**
     * 获取单个群成员信息
     *
     * @param event
     * @param qq
     * @return
     */
    public static GroupMember getGroupMember(GroupMessageEvent event, String qq) {
        Group group = event.getGroup();
        GroupMember member = null;
        ID id = null;
        if (StringUtils.isNotBlank(qq)) {
            id = SimbotUtil.getID(qq.trim());
        }
        if (Objects.isNull(id)) {
            At at = SimbotUtil.getAtInfo(event.getMessageContent());
            log.info("GroupListener.getGroupMember=====>at:{}", JSONObject.toJSONString(at));
            if (Objects.isNull(at)) {
                return null;
            }
            id = at.getTarget();
        }
        return group.getMember(id);
    }

    /**
     * 入群文字欢迎
     *
     * @param group
     * @param member
     * @param welcomeContent
     */
    public static void joinGroupTextWelcome(Group group, GroupMember member, String welcomeContent) throws MalformedURLException {
        String[] split = welcomeContent.split("\\|");
        MessagesBuilder messagesBuilder = new MessagesBuilder();
        Messages messages = messagesBuilder.at(member.getId())
                .text(split[0])
                .build();
        group.sendAsync(messages);
        if (split.length > 1) {
            LightApp app = new LightApp(split[1]);
            SimbotOriginalMiraiMessage message = new SimbotOriginalMiraiMessage(app);
            group.sendAsync(message);
        }
    }

    public static void joinGroupPictureWelcome(Group group, GroupMember member) throws IOException {
        MessagesBuilder messagesBuilder = new MessagesBuilder();
        String text = "欢迎新成员:" + member.getUsername() +
                "\n你是第" + group.getCurrentMember() + 1 + "个入群的哦~" +
                "\n" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        messagesBuilder.image(Resource.of(CommonUtil.textToPicture(text)));
        group.sendBlocking(messagesBuilder.build());
    }

}
